package com.ewanghuiju.app.ui.shopmail.activity;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ewanghuiju.app.R;
import com.ewanghuiju.app.app.Constants;
import com.ewanghuiju.app.base.SimpleActivity;
import com.ewanghuiju.app.model.bean.local.TabEntity;
import com.ewanghuiju.app.model.bean.response.IntenestTagResponBean;
import com.ewanghuiju.app.ui.main.adapter.MyPagerAdapter;
import com.ewanghuiju.app.ui.shopmail.adapter.FanliTopTagAdapter;
import com.ewanghuiju.app.ui.shopmail.fragment.FanliOrderListFragment;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.gyf.immersionbar.ImmersionBar;
import com.miguelcatalan.materialsearchview.MaterialSearchView;

import java.util.ArrayList;
import java.util.List;

import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;
import butterknife.BindView;

/**
 * @author sj  qq:751943509
 * @class 返利订单列表
 * @time 2020/2/19
 */
public class FanliOrderListActivity extends SimpleActivity {
    //    @BindView(R.id.toolbar)
//    RelativeLayout mToolbar;
    @BindView(R.id.viewpage)
    ViewPager viewpage;
    @BindView(R.id.commonTabLayout)
    CommonTabLayout commonTabLayout;
        @BindView(R.id.ctabLayout)
    CommonTabLayout ctabLayout;
    @BindView(R.id.top_recyclerView)
    RecyclerView topRecyclerView;
    @BindView(R.id.search_view)
    MaterialSearchView searchView;
    @BindView(R.id.mtoolbar)
    Toolbar mtoolbar;
    @BindView(R.id.toolbar_container)
    FrameLayout toolbarContainer;

    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
    private ArrayList<CustomTabEntity> mChildTabEntities = new ArrayList<>();

    private List<Fragment> fragmentList;
    private String[] list_Title = {"我的订单", "团队订单"};

    private String[] child_list_Title = {"\u0020\u0020全部\u0020\u0020", "已付款", "已收货", "已结算", "已失效"};

    //    private List<String> strList;
    private List<String> childStrList;

    private List<IntenestTagResponBean> intenestTagResponBeanList;

    private FanliTopTagAdapter topAdapter;

    MenuItem mSearchMenuItem;

    @Override
    protected int getLayout() {
        return R.layout.activity_fanli_order_list;
    }

    @Override
    protected void initEventAndData() {
        setSupportActionBar(mtoolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        initSearch();
        initTopRvAndFragment();
    }

    /**
     * 搜索
     */
    private void initSearch() {
        searchView.setOnQueryTextListener(new MaterialSearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                //Do some magic
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                //Do some magic
                return false;
            }
        });

        searchView.setOnSearchViewListener(new MaterialSearchView.SearchViewListener() {
            @Override
            public void onSearchViewShown() {
                //Do some magic
            }

            @Override
            public void onSearchViewClosed() {
                //Do some magic
            }
        });
    }

    private void setTopReset(List<IntenestTagResponBean> mlist) {
        if (mlist != null && mlist.size() > 0) {
            for (IntenestTagResponBean bean : mlist) {
                bean.setStatus(0);
            }
        }
    }

    private void initTopRv() {
        topRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 5));
        topAdapter = new FanliTopTagAdapter(R.layout.item_fanli_top_tag);
        topAdapter.setNewData(intenestTagResponBeanList);
        topAdapter.setHasStableIds(true);
        topAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                List<IntenestTagResponBean> intenestTagResponBeans = adapter.getData();
                setTopReset(intenestTagResponBeans);
                intenestTagResponBeans.get(position).setStatus(1);
                adapter.notifyDataSetChanged();
                viewpage.setCurrentItem(position);
            }
        });
        topRecyclerView.setAdapter(topAdapter);
    }


    private void initTopRvAndFragment() {
        fragmentList = new ArrayList<>();

        childStrList = new ArrayList<>();

        intenestTagResponBeanList = new ArrayList<>();

        for (int i = 0; i < list_Title.length; i++) {
            mTabEntities.add(new TabEntity(list_Title[i], R.mipmap.icon_selected_tab, 0));
        }
        commonTabLayout.setTabData(mTabEntities);


        for (int i = 0; i < child_list_Title.length; i++) {
            IntenestTagResponBean bean = new IntenestTagResponBean();
            bean.setText(child_list_Title[i]);
            bean.setStatus(i == 0 ? 1 : 0);
            intenestTagResponBeanList.add(bean);

            mChildTabEntities.add(new TabEntity(child_list_Title[i], R.mipmap.icon_selected_tab, 0));
            childStrList.add(child_list_Title[i]);
            FanliOrderListFragment fanliOrderListFragment = new FanliOrderListFragment();
            Bundle bundle = new Bundle();
            bundle.putInt(Constants.FANLIORDER_TYPE, i);
            fanliOrderListFragment.setArguments(bundle);
            fragmentList.add(fanliOrderListFragment);
        }

        initTopRv();  //方式一
        viewpage.setAdapter(new MyPagerAdapter(getSupportFragmentManager(), 0, fragmentList, childStrList));
        ctabLayout.setTabData(mChildTabEntities);
        ctabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                viewpage.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {
            }
        });

        viewpage.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                ctabLayout.setCurrentTab(position);
                if (topAdapter != null) {
                    List<IntenestTagResponBean> intenestTagResponBeans = topAdapter.getData();
                    setTopReset(intenestTagResponBeans);
                    intenestTagResponBeans.get(position).setStatus(1);
                    topAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        viewpage.setCurrentItem(0);
    }


//    @OnClick({R.id.iv_back, R.id.iv_fanli_serch})
//    public void doClick(View view) {
//        if (AntiShake.check(view.getId())) {    //判断是否多次点击
//            return;
//        }
//        switch (view.getId()) {
//            case R.id.iv_back:
//                finish();
//                break;
//            case R.id.iv_fanli_serch:
//                mSearchMenuItem.setVisible(true);
//                break;
//            default:
//                break;
//        }
//    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this)
                .titleBar(toolbarContainer, true)
                .statusBarDarkFont(true)
                .init();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_menu, menu);
        mSearchMenuItem = menu.findItem(R.id.action_search);
        mSearchMenuItem.setVisible(true);
        searchView.setMenuItem(mSearchMenuItem);
        return true;
    }

//    @Override
//    public void onBackPressed() {
//        if (searchView.isSearchOpen()) {
//            searchView.closeSearch();
//        } else {
//            super.onBackPressed();
//        }
//    }
    @Override
    public void onBackPressedSupport() {
        if (searchView.isSearchOpen()) {
            searchView.closeSearch();
        } else {
            super.onBackPressed();
        }
    }
}
